// 主入口文件
import { initMonthView } from './views/monthView.js';
import { initWeekView } from './views/weekView.js';
import { initDayView } from './views/dayView.js';
import { initProjectsView } from './views/projectsView.js';
import { initTagsView } from './views/tagsView.js';
import { initProjectsModule } from './modules/projects.js';
import { initTagsModule } from './modules/tags.js';
import { formatDate, formatTime, getWeekNumber } from './utils/dateUtils.js';

// 全局状态
let currentView = 'month';
let currentDate = new Date();

// DOM 元素
const views = {
  month: document.getElementById('month-view'),
  week: document.getElementById('week-view'),
  day: document.getElementById('day-view'),
  todos: document.getElementById('todos-view'),
  projects: document.getElementById('projects-view'),
  tags: document.getElementById('tags-view'),
};

const navItems = document.querySelectorAll('.nav-item');
const todoModal = document.getElementById('todo-modal');
const projectModal = document.getElementById('project-modal');
const todoForm = document.getElementById('todo-form');
const projectForm = document.getElementById('project-form');

const { invoke } = window.__TAURI__.core;
// 初始化模块
const { updateMonthView, renderMonthCalendar } = initMonthView(views, currentDate, invoke);
const { updateWeekView, renderWeekCalendar } = initWeekView(views, currentDate, invoke);
const { updateDayView, renderDayTodos } = initDayView(views, currentDate, invoke);
const projectsModule = initProjectsModule(invoke);
const { updateProjectView, loadProjectTodos } = initProjectsView(views, projectsModule.getProjects(), invoke);
const { updateTagsView } = initTagsView(views);
const { loadProjects, updateProjectSelect } = projectsModule;
const { loadTags } = initTagsModule(invoke);

// 初始化应用
async function init() {
  await loadProjects();
  await loadTags();
  updateView();
  setupEventListeners();
}

// 更新当前视图
function updateView() {
  navItems.forEach(item => {
    item.classList.toggle('active', item.dataset.view === currentView);
  });

  Object.entries(views).forEach(([view, element]) => {
    element.classList.toggle('active', view === currentView);
  });

  switch (currentView) {
    case 'month': updateMonthView(); break;
    case 'week': updateWeekView(); break;
    case 'day': updateDayView(); break;
    case 'projects': updateProjectView(); break;
    case 'tags': updateTagsView(); break;
  }
}

// 设置事件监听器
function setupEventListeners() {
  // 导航按钮点击事件
  navItems.forEach(item => {
    item.addEventListener('click', () => {
      const view = item.dataset.view;
      if (view !== currentView) {
        currentView = view;
        updateView();
      }
    });
  });

  // 其他事件监听器...
}

// 启动应用
init();
